<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;
use Common\Model\User;
use Common\Model\Goods;
use Common\Model\GoodsList;
use Common\Model\GoodsAttr;
use Common\Model\Order;
use Common\Model\OrderList;

//测试控制器
class GwcController extends Controller{
	private $userModel;
	private $goodsModel;
	private $goodsListModel;
	private $goodsAttrModel;
	private $orderModel;
	private $orderListModel;
	
	//构造函数
	public function __init()
	{
		$this->userModel=new User;
		$this->goodsModel=new Goods;
		$this->goodsListModel=new GoodsList;
		$this->goodsAttrModel=new GoodsAttr;
		$this->orderModel=new Order;
		$this->orderListModel=new OrderList;

	}
	


	public function carOne(){
		// p($_SESSION);
		$userData=null;
		if(isset($_SESSION['info'])){
			$userData=$this->userModel->where("uid={$_SESSION['info']['uid']}")->find();
		}
		View::with('userData',$userData);

		$introData=[];
		if(isset($_SESSION['cart'])){

			$gwcData=$_SESSION['cart'];
			View::with('gwcData',$gwcData);
			// p($gwcData);
			foreach ($gwcData['goods'] as $k => $v) {
				$introData[$k]=$this->goodsModel->where("gid={$v['option']}")->find();
				$introData[$k]['goodsList']=$this->goodsListModel->where("glid={$v['id']}")->find();
			}
			foreach($introData as $k =>$v){
				$temp=explode(',', $v['goodsList']['combine']);
				foreach($temp  as $kk=>$vv){
					$tempSpec[$k][$kk]=$this->goodsAttrModel->where("gtid={$vv}")->pluck('gtvalue');
				}
				$introData[$k]['tempSpec']=$tempSpec[$k];
				$introData[$k]['gwc']=$gwcData['goods'][$k];
			}


			// p($tempSpec);
			// p($introData);

		}
		View::with('introData',$introData);
		View::make();
	}





	// carOne页面的ajax方法，是点击货品清除变化的 时候 
	public function oneAjax(){
		$glid=Q('post.glid');
		// p($_SESSION);
		// 删除购物车用的是unset方法，好伤啊
		$temp=$_SESSION['cart']['goods'][$glid];
		$total_rows=$_SESSION['cart']['total_rows']-$temp['num'];
		$total=$_SESSION['cart']['total']-$temp['total'];
		// 更新原来的购物车数据
		unset($_SESSION['cart']['goods'][$glid]);
		$_SESSION['cart']['total_rows']=$total_rows;
		$_SESSION['cart']['total']=$total;
		if($_SESSION['cart']['goods']){
			$gwcData=$_SESSION['cart'];
		}else{
			$gwcData=0;
		}
		// p($gwcData);
		echo json_encode($gwcData);
		exit;
	}



// ajax提交的数据，是点击货品数量变化的时候
	public function twoAjax(){
		$num=Q('post.num',0,'intval');
		$glid=Q('post.glid');

		$data=array( 
		    'sid'=>$glid,// 唯一 sid，添加购物车时自动生成 
		    'num'=>$num 
		); 
		Cart::update($data); 
		// p($_SESSION['cart']);
		$gwcData=$_SESSION['cart'];
		echo json_encode($gwcData);
		exit;

	}


	// ajax删除选择的时候，数据变化问题
	 




		// 跳转到第二个页面
	public function carTwo(){
		if($_SESSION['cart']['goods']){
		

			// 登录者信息
			$userData=$this->userModel->where("uid={$_SESSION['info']['uid']}")->find();
			View::with('userData',$userData);
			$tempStr=Q('get.tempnam');	
			$tempStr=rtrim($tempStr,'|');
			$tempAtt=explode('|',$tempStr);
			// 购物车中被选中购买的数据信息
			$gwcData=[];
			// 展示在前台的数据信息
			$gwcMidData=[];
			$tempGwcData=Cart::getGoods();
			$total=0;
			$total_rows=0;
			foreach ($tempAtt as $k => $v){
				$glid=$tempGwcData[$v]['id'];
				$gid=$tempGwcData[$v]['option'];
				$price=$tempGwcData[$v]['price'];
				$total=$total + $tempGwcData[$v]['total'];
				$total_rows=$total_rows + $tempGwcData[$v]['num'];
				$num=$tempGwcData[$v]['num'];
				$name=$tempGwcData[$v]['name'];
				
				// 查询数据库中goods_list数据库，得到他的属性组合
				$tempCombine=$this->goodsListModel->where("glid={$glid}")->pluck('combine');
				$tempCombineArr=explode(',',$tempCombine);
				// 循环得到的属性组合，得到属性名称
				foreach($tempCombineArr as $kk =>$vv){
					$gwcMidData[$v]['goods_attr'][]=$this->goodsAttrModel->where("gtid={$vv}")->pluck('gtvalue');
				}
				$gwcMidData[$v]['gname']=$name;
				$gwcData['goods'][$v]=$tempGwcData[$v];

			}	
			$gwcData['total_rows']=$total_rows;
			$gwcData['total']=$total;
			// p($gwcData);
			// p($gwcMidData);
			View::with('gwcMidData',$gwcMidData);
			View::with('gwcData',$gwcData);
			View::make();
		}else{
			go(U('Gwc/carOne'));
		}
	}





	public function carTwoAjax(){
	
		$name=Q('post.name');
		$str=Q('post.str');
		$telephone=Q('post.telephone');
		$gwcData=Q('post.gwcData');
		$gwcMidData=Q('post.gwcMidData');
		// p($gwcData);
		// p($gwcMidData);
		$orderId=Cart::getOrderId();
		// p($orderId);
		$data=[];
		$data['number']=$orderId;
		$data['consignee']=$name;
		$data['address']=$str;
		$data['total']=$gwcData['total'];
		$data['time']=time();
		$data['status']=1;
		$data['mobile']=$telephone;
		$data['shop_user_uid']=$_SESSION['info']['uid'];
		$oid=$this->orderModel->add($data);


		foreach($gwcData['goods'] as $k=>$v){
			$olData['quantity']=$v['num'];
			$olData['subtotal']=$v['total'];
			$olData['shop_goods_gid']=$v['option'];
			$olData['shop_order_oid']=$oid;
			$olData['shop_goods_list_glid']=$v['id'];
			$this->orderListModel->add($olData);
		}

		// 在添加数据库订单表的时候 ，我们清楚购物车，就是说，用户要买商品还要重新去添加购物车
		// 这里的目的是为了少些代码工作量
		// Cart::flush(); 
		unset($_SESSION['cart']);
		echo json_encode($oid);
		exit;
	}




	// 购物车第三张页面，订单完成页面
	public function carThree(){
		// 登录者信息，其实能到这一步肯定是登录过的了
		$userData=$this->userModel->where("uid={$_SESSION['info']['uid']}")->find();
		// p($userData);
		View::with('userData',$userData);
		// 获得订单信息
		// 
		$oid=Q('get.oid',0,'intval');
		$uid=$_SESSION['info']['uid'];
		$orderData=$this->orderModel->where("oid={$oid}")->find();
		View::with('orderData',$orderData);

		// p($orderData);
		// p($_SESSION);
		View::make();
	}
}